const db = require('./db')
const inquirer = require('inquirer')

module.exports.add = async (title) => {
  // 读之前的任务
  const list = await db.read()
  // 往里面添加一个 title 任务
  list.push({title, done: false})
  // 存储任务到文件
  await db.write(list)
}

module.exports.clear = async () => {
  await db.write([])
}

function markAsDone(list,index) {
  list[index].done = true
  db.write(list)
}

function markAsUndone(list,index) {
  list[index].done = false
  db.write(list)
}

function updateTitle(list,index) {
  inquirer.prompt({
    type:'input',
    name:'title',
    message:'新的标题',
    default:list[index].title
  }).then(answer=>{
    list[index].title = answer.title
    db.write(list)
  })
}

function removeTask(list,index) {
  list.splice(index,1)
  db.write(list)
}

function askForTaskActions(list,index) {
  // 选中一个任务
  const actions = {markAsDone,markAsUndone,updateTitle,removeTask}

  inquirer.prompt({
    type: 'list', name: 'action',
    message: '请选择操作',
    choices: [
      {name: '退出', value: 'quit'},
      {name: '已完成', value: 'markAsDone'},
      {name: '未完成', value: 'markAsUndone'},
      {name: '改标题', value: 'updateTitle'},
      {name: '删除', value: 'removeTask'},
    ]
  }).then(answer2 => {
    const action = actions[answer2.action]
    action && action(list,index)
  })
}

function askForCreateTask(list) {
  // 创建任务
  inquirer.prompt({
    type:'input',
    name:'title',
    message:'输入标题',
  }).then(answer=>{
    list.push({
      title:answer.title,
      done:false
    })
    db.write(list)
  })
}

function printTasks(list){
  inquirer
    .prompt(
      {
        type: 'list',
        name: 'index',
        message: '请选择你想要操作的任务',
        choices: [
          {name: '退出', value: '-1'},
          ...list.map((task, index) => {
            return {name: `${task.done ? '[√]' : '[ ]'} ${index + 1}--${task.title}`, value: index.toString()}
          }),
          {name: '+ 创建任务', value: '-2'}
        ],
      },
    )
    .then(answer => {
      const index = parseInt(answer.index)
      if (index >= 0) {
        askForTaskActions(list,index)
      } else if (index === -2) {
        askForCreateTask(list)
      }
    })
}

module.exports.showAll = async () => {
  //  读取之前写的任务
  const list = await db.read()
  // 打印出来之前写的任务
 printTasks(list)
}
